﻿using AutoMapper;
using PowerHub.MES.Common.NumberingCenter;
using PowerHub.MES.DTO.Shopfloor.DTO;
using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.IServices.IShopfloor;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.IServices.IEquipment;
using PowerHub.MES.Entities.Equipment;
using PowerHub.MES.DTO.Equipment.QueryDTO;
using PowerHub.MES.DTO.Equipment.DTO;
using PowerHub.MES.IRepository.IEquipment;

namespace PowerHub.MES.Services.Equipment
{

    /// <summary>
    /// 设备管理接口实现层
    /// </summary>
    public class Equipment_EquipmentManagementServices : BaseServices<Equipment_EquipmentManagement>, IEquipment_EquipmentManagementServices
    {
        private IEquipment_EquipmentManagementRepository _iEquipment_EquipmentManagementRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public Equipment_EquipmentManagementServices(PowerHubMesContext powerHubMesContext, IEquipment_EquipmentManagementRepository iEquipment_EquipmentManagementRepository, UserServices userServices, IMapper mapper) : base(powerHubMesContext)
        {
            _iEquipment_EquipmentManagementRepository = iEquipment_EquipmentManagementRepository;
            _userServices = userServices;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取设备管理列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<Equipment_EquipmentManagement>>> GetEquipment_EquipmentManagementPageListAsync(QueryEquipment_EquipmentManagementDTO query)
        {
            var ss = _userServices.UserId;
            var UserName = _userServices.UserName;
            var Role = _userServices.Role;
            return await _iEquipment_EquipmentManagementRepository.GetEquipment_EquipmentManagementPageListAsync(query);
        }

        /// <summary>
        /// 添加设备管理
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateEquipment_EquipmentManagementAsync(Equipment_EquipmentManagementDTO query)
        {
            Equipment_EquipmentManagement foundation = await GetEntityAsync(d => d.EquipmentName.Equals(query.EquipmentName) && d.IsDel == 0);
            if (foundation != null)
            {
                throw new Exception("你输入的设备管理名称已存在！");
            }
            //实体转换
            var Equipment_EquipmentManagement = _mapper.Map<Equipment_EquipmentManagement>(query);
            Equipment_EquipmentManagement.Id = Guid.NewGuid().ToString();
            Equipment_EquipmentManagement.EquipmentNo = string.IsNullOrEmpty(query.EquipmentNo) || query.EquipmentNo.Contains(" ") ? GenerateBasicDataNO.GenerateCode(query.EquipmentName) : query.EquipmentNo;
            Equipment_EquipmentManagement.Status = 0;
            Equipment_EquipmentManagement.Creator = _userServices.UserName;

            return await _iEquipment_EquipmentManagementRepository.CreateEquipment_EquipmentManagementAsync(Equipment_EquipmentManagement);
        }



        /// <summary>
        /// 批量删除设备管理
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionEquipment_EquipmentManagementAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _iEquipment_EquipmentManagementRepository.DeletionEquipment_EquipmentManagementAsync(ids);
        }


        /// <summary>
        /// 更新设备管理
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateEquipment_EquipmentManagementAsync(Equipment_EquipmentManagementDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取设备管理信息
            Equipment_EquipmentManagement Equipment_EquipmentManagement = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (Equipment_EquipmentManagement == null)
            {
                throw new Exception("更新设备管理信息发生了异常,请刷新页面后试试");
            }
            Equipment_EquipmentManagement.EquipmentName = query.EquipmentName;
            Equipment_EquipmentManagement.Model = query.Model;
            Equipment_EquipmentManagement.EquipmentClassifyId = query.EquipmentClassifyId;
            Equipment_EquipmentManagement.Description = query.Description;
            Equipment_EquipmentManagement.Updator = _userServices.UserName;


            return await _iEquipment_EquipmentManagementRepository.UpdateEquipment_EquipmentManagementAsync(Equipment_EquipmentManagement);

        }
    }
}
